package com.lenovo.nova.widget.ablistview;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;

import android.widget.FrameLayout;
import com.lenovo.nova.util.Applications;
import com.lenovo.nova.util.DebugUtil;
import com.lenovo.nova.util.debug.mylog;
import com.lenovo.nova.utils.R;
import com.lenovo.nova.widget.ablistview.TwoWayAbsListView.OnScrollListener;

public class ArrowHorizontalGridView extends TwoWayGridView implements OnScrollListener ,
        OnGlobalLayoutListener {

	private static String TAG = ArrowHorizontalGridView.class.getSimpleName();


	private int mNumRows;
	private int nusColumn;

    private boolean isVisibleToUser;

    private OnArrowChangeListener mOnArrowChangeListener;
    private int firstVisibleItem;

    private static boolean isDebugMode;

    private HoverBigWindowManager mHoverBigManager ;

    private OnHorizontalScrollListener mOnScrollListener;

    public ArrowHorizontalGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context);
//		setScrollbarFadingEnabled(false);
	}

	public ArrowHorizontalGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TwoWayGridView);
        boolean isDebugMode = a.getBoolean(R.styleable.TwoWayGridView_debugMode,false);
        a.recycle();
        setDebugMode(isDebugMode);
	}

    @Override
    public void setSelection(int position) {
        super.setSelection(position);
        mylog.i(TAG,"setSelection position " + position);
    }

    @Override
    protected void obtainView(int position, View convertView, TwoWayAbsListView twoWayAbsListView) {
        if(isDebugMode){
            convertView.setBackgroundColor(DebugUtil.getRandomColor());


            View firstView = getChildAt(0);
            if(firstView != null){
                int column = getNumColumn();

                int horizontalSpace = (getWidth() - column * firstView.getWidth()) / ( column- 1);
                int suggestWidth = column * firstView.getWidth() + (column -1)  * getHorizontalSpacing() ;
                mylog.i(TAG,"column " + column +
                        " suggest horizontalSpace is " + horizontalSpace +
                        " getHorizontalSpacing() " + getHorizontalSpacing() +
                        " firstView width " + firstView.getWidth() +
                        " suggestWidth " + (suggestWidth + 16)
                );

            }
        }
    }

    @Override
	public void setNumRows(int numRows) {
		super.setNumRows(numRows);
		mNumRows = numRows;
	}

    @Override
	public void setNumColumns(int numColumns) {
		super.setNumColumns(numColumns);
        nusColumn = numColumns;
	}

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        setOnScrollListener(this);
        arrow.onAttachedToWindow();
        mylog.i(TAG, "onAttachedToWindow " + " isVisibleToUser()" + isVisibleToUser() );

        if(isDebugMode){
            setBackgroundColor(Color.parseColor("#df34f4"));
        }
        mHoverBigManager.onAttachedToWindow(getContext());
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        super.setOnScrollListener(null);
        arrow.onDetachedFromWindow();
        mylog.i(TAG, "onDetachedFromWindow " + " isVisibleToUser()" + isVisibleToUser());
        mHoverBigManager.onDetachedFromWindow();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public boolean onHoverEvent(MotionEvent event) {
        mHoverBigManager.onHoverEvent(event);



        return super.onHoverEvent(event);
    }

    @SuppressLint("WrongCall")
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        mylog.d(TAG, "onLayout changed " + changed + " l " + l + " t " + t + " r " + r + " b " + b);

        if(isVisibleToUser){
            arrow.onLayout(changed, l, t, r, b);
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        mylog.d(TAG, "onScrollChanged l " + l + " t " + t + " oldl " + oldl + " oldt " + oldt);
        if(isVisibleToUser){
            arrow.onScrollChanged(l, t, oldl, oldt);
        }
    }

    @Override
    public void onScrollStateChanged(TwoWayAbsListView view, int scrollState) {
        mylog.i(TAG, "onScrollStateChanged scrollState "+ scrollState);


        if(scrollState == OnScrollListener.SCROLL_STATE_IDLE){
            if(Applications.get().isInTouchMode()){
//                setSelectedPositionInt(firstVisibleItem);

            }
        }else {
            if(Applications.get().isInTouchMode()){
                hideSelector();
            }
        }

        if(isVisibleToUser){
            arrow.onScrollStateChanged(view,scrollState);
        }

        if(mOnScrollListener != null){
            mOnScrollListener.onScrollStateChanged(view,scrollState);
        }
    }

    @Override
    public void onScroll(TwoWayAbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        isVisibleToUser = isVisibleToUser();

        mylog.d(TAG, "onScroll firstVisibleItem "+firstVisibleItem +
                        " visibleItemCount " + visibleItemCount +
                        " totalItemCount " + totalItemCount +
                        " isVisibleToUser " + isVisibleToUser
        );

        this.firstVisibleItem = firstVisibleItem;

        if(isVisibleToUser){
            arrow.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
        }

        if(mOnScrollListener != null){
            mOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
        }
    }

    @Override
    public void onGlobalLayout() {
        mylog.d(TAG, "onGlobalLayout " + isVisibleToUser + "  " + getVisibility());
        isVisibleToUser = isVisibleToUser();
        if(isVisibleToUser){
            arrow.onGlobalLayout();
        }
    }

    public void setOnArrowChangeListener(OnArrowChangeListener l){
        mOnArrowChangeListener = l;
    }

    public void setOnHorizontalArrowClickListener(
            HorizontalArrowBuilderHelper.OnHorizontalArrowClickListener l){
        arrow.setOnHorizontalArrowClickListener(l);
    }

    public int getNumColumn() {
		return nusColumn;
	}

	public int getNumRows(){
		return mNumRows;
	}

	public ArrowHorizontalGridView(Context context) {
		super(context);
		initView(context);
	}
	
	private void initView(Context context){
		arrow.setHostView(this);

        setSmoothScrollbarEnabled(false);
        setHorizontalScrollBarEnabled(false);
        mHoverBigManager = new HoverBigWindowManager(this);
	}

    public void setOnScrollListener(OnHorizontalScrollListener l){
        mOnScrollListener = l;
    }

    public void setDebugMode(boolean debugMode){
        isDebugMode = debugMode;
    }

    public HorizontalArrowBuilderHelper getArrowHelper(){
        return arrow;
    }

    public HorizontalArrowBuilderHelper arrow = new HorizontalArrowBuilderHelper(){
        @Override
        protected void onSetShowLeftArrow() {
            if(mOnArrowChangeListener != null){
                if(mOnArrowChangeListener.onLeftShowRightHide()){
                    return;
                }
            }
            super.onSetShowLeftArrow();
        }

        @Override
        protected void onSetShowRightArrow() {
            if(mOnArrowChangeListener != null){
                if(mOnArrowChangeListener.onLeftHideRightShow()){
                    return;
                }
            }
            super.onSetShowRightArrow();
        }

        @Override
        protected void onSetTwoArrowVisible(boolean visible) {
            if(mOnArrowChangeListener != null){
                if(mOnArrowChangeListener.onSetTwoArrowVisible(visible)){
                    return;
                }
            }
            super.onSetTwoArrowVisible(visible);
        }

    };

    public interface OnArrowChangeListener{
        boolean onLeftShowRightHide();
        boolean onLeftHideRightShow();
        boolean onSetTwoArrowVisible(boolean visible);
    }

    public interface OnHorizontalScrollListener {
        public void onScrollStateChanged(ViewGroup view, int scrollState);

        public void onScroll(ViewGroup view, int firstVisibleItem, int visibleItemCount,
                             int totalItemCount);
    }
}
